/**
 * Copyright (c) 2023 myons Inc. All rights reserved.
 */
package cn.zmwh.common.core.config;

import cn.hutool.http.HttpGlobalConfig;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.TimeUnit;

/**
 * @describe: ok http配置 、
 * @author: dmzmwh 、
 * @time: 2023-04-12 10:05 、
 */
@Configuration
public class OkHttpClientConfig implements ApplicationRunner {

    @Bean(name = "okHttpClient")
    public OkHttpClient okHttpClient() {
        OkHttpClient client = new OkHttpClient.Builder().readTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS)
                .connectionPool(new ConnectionPool(2000, 200, TimeUnit.MINUTES)).retryOnConnectionFailure(true).build();
        return client;
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        //hutool http 设置读取超时时间（单位：毫秒）
        HttpGlobalConfig.setTimeout(15000);
    }
}
